import { Terser, defineConfig } from 'vite'
import path from 'path'
import vue from '@vitejs/plugin-vue'
// 配置打包压缩文件
import viteCompression from 'vite-plugin-compression'
// 配置兼容低版本浏览器
import legacyPlugin from '@vitejs/plugin-legacy'
// 组件按需导入
const pathSrc = path.resolve(__dirname, 'src')
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
export default defineConfig(()=>{
    return {
        plugins: [
            vue(),
            viteCompression({
                verbose: true,
                disable: false,
                threshold: 10240,
                algorithm: 'gzip',
                ext: '.gz',
            }),
            legacyPlugin({
                targets: ['chrome 52'],
                additionalLegacyPolyfills: ['regenerator-runtime/runtime'] // 面向IE11时需要此插件
            }),
            AutoImport({
                imports:['vue'],
                resolvers:[ElementPlusResolver()],
                dts: path.resolve(pathSrc, 'auto-imports.d.ts')
            }),
            Components({
                resolvers: [ElementPlusResolver()],
                dts: path.resolve(pathSrc, 'components.d.ts')
            })
        ],
        resolve: {
            alias: {
                "@": path.resolve('./src')
            },
            extensions:['.js','.ts','.vue']
        },
        // scss全局变量配置
        css:{
            preprocessorOptions:{
                scss:{
                    javascriptEnabled: true,
                    additionalData: '@import "./src/assets/styles/variable.scss";'
                }
            }
        },
        build:{
            rollupOptions:{
                output:{
                    chunkFileNames: 'static/js/[name]-[hash].js',
                    entryFileNames: 'static/js/[name]-[hash].js',
                    assetFileNames: 'static/[ext]/[name]-[hash].[ext]'
            }
        }
    }
}
})
